var fs = require("fs");
var _ = require("lodash");

let fileNameB = "../extra";
let fileNameC = "./itemDes";
let outputFileName = "./lua/item.lua";

let extraData = JSON.parse(fs.readFileSync(`${fileNameB}.json`).toString()).item;
let mainData = JSON.parse(fs.readFileSync(`${fileNameC}.json`).toString()).map;

let extraMap = new Map();
_.forEach(extraData, (e) => {
    let id = e.id;
    extraMap.set(id, e);
});

let output = `local p = {}
`

_.forEach(mainData, (e) => {
    let data = e.data;
    let str = `p['${e.id}'] = {
    ['name'] = '${data.name}',
    ['des'] = [[${data.description}]],
    `
    if (extraMap.has(e.id)) {
        let data = extraMap.get(e.id);
        if (data.icon){
            str += `['icon'] = '${data.icon}',
    `   }
        if (data.nickname){
            str += `['nickname'] = '${data.nickname}',
    `   }
    }
    str += `}

`;
    output += str;
});
output += `return p`

fs.writeFileSync(outputFileName, output);
